package com.quark.rest.service.impl;

import com.quark.common.base.BaseServiceImpl;
import com.quark.common.dao.LabelDao;
import com.quark.common.entity.Label;
import com.quark.rest.service.LabelService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * @Author LHR
 * Create By 2017/8/27
 */
@Service
public class LabelServiceImpl extends BaseServiceImpl<LabelDao,Label> implements LabelService{

    @Override
    public Label findByName(String name) {
        return repository.findByName(name);
    }

    @Override
    public List<Label> findByParentLableId(Integer labelId) {
        return repository.findByParentLableId(labelId);
    }

    @Override
    public Label getRandom() {
        List<Label> labels = repository.findAll();
        if (labels != null) {
            int randomInt = (new Random().nextInt());
            if (randomInt < 0) {
                randomInt = - randomInt;
            }
            return  labels.get(randomInt % labels.size());
        }
        return null;
    }
}
